home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 13 / MacFormat n. 13 (Spain) / Macformat 13.bin / Shareware Internet / Educación / xComputer 1.2 / Examples / Example 6 - Interrupts < prev   
Encoding:
Text File  |  1995-12-29  |  7.0 KB  |  252 lines

  1. ; Example 6: Interrupts.
  2.  
  3. ; This example illustrates the use of an interrupt handler.
  4. ; To use it, you must turn on the "Use I/O Services" in the
  5. ; Options menu.  You might want to run the program at
  6. ; "Fastest" speed with the Memory Display set to "Graphics".
  7.  
  8. ; The program is really a combination of two previous
  9. ; examples, Example 3 and Example 5.  The computer runs
  10. ; the program from example 3.  However, when you type a
  11. ; character, an "interrupt handler" is called to process
  12. ; the character you typed.  The processing is the same as
  13. ; what is done in Example 5.  What the user of the program
  14. ; sees is the calculator program of Example 5, with the
  15. ; powers-of-three program from Example 3 running in the
  16. ; "background".
  17.  
  18. ; For the best effect, run the program at Fastest speed in
  19. ; Graphics memory display mode.  Type numbers separated by
  20. ; "+", "-", and "=".
  21.  
  22. @PC 0   ; Make sure the PC contains 0, the starting location
  23.         ;   of the program.
  24.  
  25. 1024# 0 ; Clear memory before loading
  26.  
  27. @0      ; Start loading program at location 0
  28.  
  29.         lod-c 0   ; Set up the starting state
  30.         sto num
  31.         sto sum
  32.         lod-c '+
  33.         sto op
  34.  
  35.         inh calc  ; Now, set up an interrupt handler at
  36.                   ;   location calc.  The computer will jump
  37.                   ;   to this location to handle characters
  38.                   ;   typed by the user.
  39.  
  40.         jmp bkg   ; Jump to the "background" program at
  41.                   ;    location "bkg"; this program is
  42.                   ;    executed only if there is no input
  43.                   ;    to be processed.
  44.  
  45. ;------------------------------------------------------------
  46.  
  47. @100  ; Load the calculator program at location 100
  48.  
  49. calc:   gtc       ; Get a typed character from the input queue;
  50.                   ;    there HAS to be one, since the compuer only
  51.                   ;    jumps to this location when a character
  52.                   ;    has been typed
  53.         sto ch    ; Save the typed character in "ch".
  54.  
  55.         sub-c '+  ; Test the typed character.  If it is
  56.         jmz do_op ;    +, -, or =, then jump to "do_op".
  57.         lod ch    ;    If it is a digit between 0 and 9,
  58.         sub-c '-  ;    jump to "digit".  Otherwise, it is
  59.         jmz do_op ;    illegal input; jump to "bad".
  60.         lod ch
  61.         sub-c '=
  62.         jmz do_op
  63.         lod ch
  64.         sub-c '0
  65.         jmn bad
  66.         lod ch
  67.         sub-c '9
  68.         jmn digit
  69.         jmz digit
  70.  
  71. bad:    lod-c '?    ; Illegal input was typed.  Output the
  72.         ptc         ;     chatacter "?" followed by a space,
  73.         lod-c 32    ;     then return the calculator to its
  74.         ptc         ;     starting state.
  75.         jmp restart
  76.  
  77. digit:  lod ch      ; A digit was typed.
  78.         sub-c '0    ; Convert the character to the corresponding
  79.         sto ch      ;     number and put that number back in ch.
  80.         lod num     ; Set num := 10*num + ch, effectively adding
  81.         shl         ;     the typed digit onto the end of num.
  82.         shl         ;     (10*num is computed by shifting num
  83.         shl         ;     left three times, giving 8*num, and
  84.         add num     ;     then adding num twice to that.)
  85.         add num
  86.         add ch
  87.         sto num
  88.         rti         ; After processing the input digit, end
  89.                     ;    the interrupt handler with a return-
  90.                     ;    from-interrupt instruction
  91.  
  92. do_op:  lod num     ; A "+", "-", or "=" was typed.  Output
  93.         ptu         ;    the current value of "num", and then
  94.         lod op      ;    either add "num" to "sum" or subtract
  95.         sub-c '+    ;    it from "sum", depending on the
  96.         jmz plus    ;    pending operation, "op".
  97. minus:  lod sum
  98.         sub num
  99.         sto sum
  100.         jmp fin_op
  101. plus:   lod sum
  102.         add num
  103.         sto sum
  104. fin_op: lod-c 0     ; After the calculation, "num" is reset to 0
  105.         sto num     ;    so that the user can start typing the
  106.         lod ch      ;    next number.  "Ch", the operation that
  107.         ptc         ;    was just input, is printed.
  108.         sub-c '=    ; If "ch" is "+" or "-", then it is
  109.         jmz equals  ;    stored in "op" to become the next
  110.         lod ch      ;    pending operation.
  111.         sto op
  112.         rti         ; Return-from-interrupt
  113.  
  114. equals: lod sum     ; If the typed character was "=", then
  115.         ptu         ;    the final answer, "sum" is output,
  116.         lod-c 32    ;    followed by a space (ASCII 32) to
  117.         ptc         ;    separate computations in the output list.
  118.         jmp restart ; Then the calculator is returned to its
  119.                     ;    starting state to get ready for the
  120.                     ;    next computation.
  121.  
  122. restart: lod-c 0    ; Restore the starting state of the calculator
  123.          sto num    ;    and return from the interrupt handler
  124.          sto sum
  125.          lod-c '+
  126.          sto op
  127.          rti
  128. op:     data    ; Pending operation, that is, a "+" or "-"
  129.                 ;    that will be performed after its second
  130.                 ;    operand has been entered.
  131. ch:     data    ; Holds the character typed most recently
  132.                 ;    by the user.
  133. num:    data    ; Holds a number as itis being typed by
  134.                 ;    the user.
  135. sum:    data    ; Holds the result of any computation 
  136.                 ;    performed so far, while the user is
  137.                 ;    typing the next number.
  138.  
  139.  
  140. ;------------------------------------------------------------
  141.  
  142. @300  ; Load the "background" program at location 200
  143.  
  144. bkg:   lod-c 3    ; The program itself...
  145.        sto N1
  146.        lod-c 1
  147.        sto ct1
  148.  
  149. copy:  lod ct1
  150.        sto ct2
  151.        sto ct
  152.        lod-c N1
  153.        sto src
  154.        lod-c N2
  155.        sto dest
  156. c1:    lod-i src
  157.        sto-i dest
  158.        lod ct
  159.        dec
  160.        jmz sm
  161.        sto ct
  162.        lod src
  163.        dec
  164.        sto src
  165.        lod dest
  166.        dec
  167.        sto dest
  168.        jmp c1
  169.  
  170. sm:    lod ct2
  171.        sto ct
  172.        lod-c N2
  173.        sto src
  174.        lod-c N1
  175.        sto dest
  176.        lod-c 0
  177.        sto carryQ
  178. s1:    lod-i dest
  179.        add-i src
  180.        sto-i dest
  181.        jmf cr1
  182. s2:    lod-i dest
  183.        add-i src
  184.        sto-i dest
  185.        jmf cr2
  186. s3:    lod ct
  187.        dec
  188.        jmz copy
  189.        sto ct
  190.        lod src
  191.        dec
  192.        sto src
  193.        lod dest
  194.        dec
  195.        sto dest
  196.        jmp s1
  197.  
  198. cr1:   lod-c s2
  199.        sto return
  200.        jmp add1
  201.  
  202. cr2:   lod-c s3
  203.        sto return
  204.        jmp add1
  205.  
  206. add1:  lod ct
  207.        sto aCt
  208.        lod dest
  209.        sto aDest
  210. a1:    lod aDest
  211.        dec
  212.        sto aDest
  213.        lod aCt
  214.        dec
  215.        sto aCt
  216.        jmz advnc
  217.        lod-i aDest
  218.        add-c 1
  219.        sto-i aDest
  220.        jmf a1
  221.        jmp-i return
  222. advnc: lod carryQ
  223.        jmz adv1
  224.        lod-i aDest
  225.        add-c 1
  226.        sto-i aDest
  227.        jmp-i return
  228. adv1:  lod ct1
  229.        inc
  230.        sto ct1
  231.        lod-c 1
  232.        sto-i aDest
  233.        lod-c 1
  234.        sto carryQ
  235.        jmp-i return
  236.  
  237. return:data     ; Data locations used by the program in its
  238. ct1:   data     ;    calculations
  239. ct2:   data
  240. ct:    data
  241. src:   data
  242. dest:  data
  243. aCt:   data
  244. aDest: data
  245. carryQ:data
  246.  
  247. @700         ; "N1" will refer to location 700
  248. N1: data
  249.  
  250. @1000        ; "N2" will refer to location 1000
  251. N2: data
  252.